{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfd6873e-6b81-4eb8-af28-e72d4b3417a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import graphviz\n",
    "import lingam\n",
    "from lingam.utils import make_dot\n",
    "import logging\n",
    "import dowhy\n",
    "from dowhy import CausalModel\n",
    "from semopy import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e1aa675-00f2-4970-b56c-5486fcd03643",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"\\df.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ee4009f-2ac8-4db0-a5da-947416816442",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.columns.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "709ee70d-e3a1-42b7-8381-f91aad83ef6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the SEM model\n",
    "model_desc = \"\"\"\n",
    "\n",
    "Job_threat_from_defectors ~ Perceived_living_improvement + income + education + sex + age + marriage + ideology + religion\n",
    "Job_threat_from_defectors ~ Contact_with_defectors + income + education + sex + age + marriage + ideology + religion\n",
    "\n",
    "Attitude_toward_defector_acceptance ~ Job_threat_from_defectors + income + education + sex + age + marriage + ideology + religion\n",
    "\"\"\"\n",
    "# Load your data (make sure 'df' is already defined or load it accordingly)\n",
    "# Example:\n",
    "# df = pd.read_csv(\"your_data.csv\")\n",
    "\n",
    "# Create and fit the model\n",
    "model = Model(model_desc)\n",
    "results = model.fit(df)\n",
    "params = model.inspect()\n",
    "\n",
    "# Print full SEM results\n",
    "print(\"Full SEM parameter estimates:\\n\")\n",
    "print(params)\n",
    "\n",
    "# Optionally display in tabular format (useful for Jupyter)\n",
    "pd.set_option('display.max_rows', None)\n",
    "pd.set_option('display.max_columns', None)\n",
    "display(params)\n",
    "\n",
    "# Export to CSV if desired\n",
    "params.to_csv(\"sem_full_results.csv\", index=False)\n",
    "\n",
    "# Grouped output for interpretability\n",
    "print(\"\\n--- Grouped Results by Outcome Variable ---\")\n",
    "outcomes = ['Distrust', 'Indifference', 'Loneliness']\n",
    "for target in outcomes:\n",
    "    subset = params[(params['op'] == '~') & (params['lval'] == target)]\n",
    "    if not subset.empty:\n",
    "        print(f\"\\nResults for {target}:\\n\", subset[['lval', 'op', 'rval', 'Estimate', 'Std.Err', 'z-value', 'p-value']])\n",
    "\n",
    "# Function to visualize the SEM model with selected variables\n",
    "def visualize_sem_with_focused_params(model, params, focus_vars, path=\"sem_graph\", width=\"80\", height=\"120\", fontsize=\"14\"):\n",
    "    dot = graphviz.Digraph(comment='Focused SEM Model')\n",
    "    dot.attr(size=f\"{width},{height}\", ratio='auto')\n",
    "\n",
    "    focus_set = set(focus_vars)\n",
    "\n",
    "    # Add nodes\n",
    "    for var in focus_set:\n",
    "        dot.node(var, var, shape='ellipse')\n",
    "\n",
    "    # Add edges with estimates and significance\n",
    "    for _, row in params.iterrows():\n",
    "        if row['op'] == '~' and {row['lval'], row['rval']}.issubset(focus_set):\n",
    "            estimate = round(row['Estimate'], 3)\n",
    "            p_value = round(row['p-value'], 3)\n",
    "            significance = '***' if p_value < 0.01 else '**' if p_value < 0.05 else '* ' if p_value < 0.1 else ''\n",
    "            label = f'est: {estimate}, p: {p_value} {significance}'\n",
    "            dot.edge(row['rval'], row['lval'], label=label, fontsize=fontsize)\n",
    "\n",
    "    # Render and save\n",
    "    dot.render(path, view=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e76a7cc-3c93-49b7-ae54-fc2fa131cc13",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
